| Conditions | 7 |
| Paths | 16 |
| Total Lines | 380 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | (function () { |
||
| 15 | function ($scope, VaultService, SettingsService, $location, CredentialService, $rootScope, FileService, EncryptService, TagService, $timeout, NotificationService, CacheService, ShareService, SharingACL, $interval, $filter, $routeParams) { |
||
| 16 | $scope.active_vault = VaultService.getActiveVault(); |
||
| 17 | if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) { |
||
| 18 | if (!$scope.active_vault) { |
||
| 19 | $location.path('/'); |
||
| 20 | } |
||
| 21 | } else { |
||
| 22 | if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) { |
||
| 23 | var _vault = angular.copy(SettingsService.getSetting('defaultVault')); |
||
| 24 | _vault.vaultKey = angular.copy(SettingsService.getSetting('defaultVaultPass')); |
||
| 25 | VaultService.setActiveVault(_vault); |
||
| 26 | $scope.active_vault = _vault; |
||
| 27 | //@TODO check if vault exists |
||
| 28 | } |
||
| 29 | |||
| 30 | } |
||
| 31 | |||
| 32 | $scope.show_spinner = true; |
||
| 33 | var fetchCredentials = function () { |
||
| 34 | VaultService.getVault({guid: $routeParams.vault_id}).then(function (vault) { |
||
| 35 | var vaultKey = angular.copy($scope.active_vault.vaultKey); |
||
| 36 | var _credentials = angular.copy(vault.credentials); |
||
| 37 | vault.credentials = []; |
||
| 38 | $scope.active_vault = vault; |
||
| 39 | $scope.active_vault.vaultKey = vaultKey; |
||
| 40 | VaultService.setActiveVault($scope.active_vault); |
||
| 41 | for (var i = 0; i < _credentials.length; i++) { |
||
| 42 | var _credential = _credentials[i]; |
||
| 43 | try { |
||
| 44 | if (!_credential.shared_key) { |
||
| 45 | _credential = CredentialService.decryptCredential(angular.copy(_credential)); |
||
| 46 | |||
| 47 | } else { |
||
| 48 | var enc_key = EncryptService.decryptString(_credential.shared_key); |
||
| 49 | _credential = ShareService.decryptSharedCredential(angular.copy(_credential), enc_key); |
||
| 50 | } |
||
| 51 | _credential.tags_raw = _credential.tags; |
||
| 52 | } catch (e) { |
||
| 53 | NotificationService.showNotification('An error happend during decryption', 5000); |
||
| 54 | //$rootScope.$broadcast('logout'); |
||
| 55 | //SettingsService.setSetting('defaultVaultPass', null); |
||
| 56 | //.setSetting('defaultVault', null); |
||
| 57 | //$location.path('/') |
||
| 58 | |||
| 59 | } |
||
| 60 | if (_credential.tags) { |
||
| 61 | TagService.addTags(_credential.tags); |
||
| 62 | } |
||
| 63 | _credentials[i] = _credential; |
||
| 64 | } |
||
| 65 | |||
| 66 | ShareService.getCredendialsSharedWithUs(vault.guid).then(function (shared_credentials) { |
||
| 67 | for (var c = 0; c < shared_credentials.length; c++) { |
||
| 68 | var _shared_credential = shared_credentials[c]; |
||
| 69 | var decrypted_key = EncryptService.decryptString(_shared_credential.shared_key); |
||
| 70 | var _shared_credential_data; |
||
| 71 | try { |
||
| 72 | _shared_credential_data = ShareService.decryptSharedCredential(_shared_credential.credential_data, decrypted_key); |
||
| 73 | } catch (e) { |
||
|
|
|||
| 74 | |||
| 75 | } |
||
| 76 | if (_shared_credential_data) { |
||
| 77 | delete _shared_credential.credential_data; |
||
| 78 | _shared_credential_data.acl = _shared_credential; |
||
| 79 | _shared_credential_data.acl.permissions = new SharingACL(_shared_credential_data.acl.permissions); |
||
| 80 | _shared_credential_data.tags_raw = _shared_credential_data.tags; |
||
| 81 | if (_shared_credential_data.tags) { |
||
| 82 | TagService.addTags(_shared_credential_data.tags); |
||
| 83 | } |
||
| 84 | _credentials.push(_shared_credential_data); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | angular.merge($scope.active_vault.credentials, _credentials); |
||
| 88 | $scope.show_spinner = false; |
||
| 89 | }); |
||
| 90 | }); |
||
| 91 | }; |
||
| 92 | |||
| 93 | var getPendingShareRequests = function () { |
||
| 94 | ShareService.getPendingRequests().then(function (shareRequests) { |
||
| 95 | if (shareRequests.length > 0) { |
||
| 96 | $scope.incoming_share_requests = shareRequests; |
||
| 97 | jQuery('.share_popup').dialog({ |
||
| 98 | width: 600, |
||
| 99 | position: ['center', 90] |
||
| 100 | }); |
||
| 101 | } |
||
| 102 | }); |
||
| 103 | }; |
||
| 104 | |||
| 105 | |||
| 106 | var refresh_data_interval = null; |
||
| 107 | if ($scope.active_vault) { |
||
| 108 | $scope.$parent.selectedVault = true; |
||
| 109 | fetchCredentials(); |
||
| 110 | getPendingShareRequests(); |
||
| 111 | refresh_data_interval = $interval(function () { |
||
| 112 | fetchCredentials(); |
||
| 113 | getPendingShareRequests(); |
||
| 114 | }, 60000 * 5); |
||
| 115 | } |
||
| 116 | $scope.$on('$destroy', function () { |
||
| 117 | $interval.cancel(refresh_data_interval); |
||
| 118 | }); |
||
| 119 | |||
| 120 | |||
| 121 | $scope.permissions = new SharingACL(0); |
||
| 122 | |||
| 123 | $scope.hasPermission = function (acl, permission) { |
||
| 124 | if (acl) { |
||
| 125 | var tmp = new SharingACL(acl.permission); |
||
| 126 | return tmp.hasPermission(permission); |
||
| 127 | } else { |
||
| 128 | return true; |
||
| 129 | } |
||
| 130 | |||
| 131 | }; |
||
| 132 | |||
| 133 | $scope.acceptShareRequest = function (share_request) { |
||
| 134 | var crypted_shared_key = share_request.shared_key; |
||
| 135 | var private_key = EncryptService.decryptString(VaultService.getActiveVault().private_sharing_key); |
||
| 136 | |||
| 137 | private_key = ShareService.rsaPrivateKeyFromPEM(private_key); |
||
| 138 | crypted_shared_key = private_key.decrypt(forge.util.decode64(crypted_shared_key)); |
||
| 139 | crypted_shared_key = EncryptService.encryptString(crypted_shared_key); |
||
| 140 | |||
| 141 | ShareService.saveSharingRequest(share_request, crypted_shared_key).then(function (result) { |
||
| 142 | var idx = $scope.incoming_share_requests.indexOf(share_request); |
||
| 143 | $scope.incoming_share_requests.splice(idx, 1); |
||
| 144 | var active_share_requests = false; |
||
| 145 | for (var v = 0; v < $scope.incoming_share_requests.length; v++) { |
||
| 146 | if ($scope.incoming_share_requests[v].target_vault_id === $scope.active_vault.vault_id) { |
||
| 147 | active_share_requests = true; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | if (active_share_requests === false) { |
||
| 151 | jQuery('.ui-dialog').remove(); |
||
| 152 | fetchCredentials(); |
||
| 153 | } |
||
| 154 | }); |
||
| 155 | }; |
||
| 156 | |||
| 157 | $scope.declineShareRequest = function (share_request) { |
||
| 158 | ShareService.declineSharingRequest(share_request).then(function () { |
||
| 159 | var idx = $scope.incoming_share_requests.indexOf(share_request); |
||
| 160 | $scope.incoming_share_requests.splice(idx, 1); |
||
| 161 | var active_share_requests = false; |
||
| 162 | for (var v = 0; v < $scope.incoming_share_requests.length; v++) { |
||
| 163 | if ($scope.incoming_share_requests[v].target_vault_id === $scope.active_vault.vault_id) { |
||
| 164 | active_share_requests = true; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | if (active_share_requests === false) { |
||
| 168 | jQuery('.ui-dialog').remove(); |
||
| 169 | fetchCredentials(); |
||
| 170 | } |
||
| 171 | }); |
||
| 172 | }; |
||
| 173 | |||
| 174 | |||
| 175 | $scope.addCredential = function () { |
||
| 176 | var new_credential = CredentialService.newCredential(); |
||
| 177 | var enc_c = CredentialService.encryptCredential(new_credential); |
||
| 178 | SettingsService.setSetting('edit_credential', enc_c); |
||
| 179 | $location.path('/vault/' + $scope.active_vault.guid + '/new'); |
||
| 180 | }; |
||
| 181 | |||
| 182 | $scope.editCredential = function (credential) { |
||
| 183 | var _credential = angular.copy(credential); |
||
| 184 | $rootScope.$emit('app_menu', false); |
||
| 185 | SettingsService.setSetting('edit_credential', CredentialService.encryptCredential(_credential)); |
||
| 186 | $location.path('/vault/' + $scope.active_vault.guid + '/edit/' + _credential.guid); |
||
| 187 | }; |
||
| 188 | |||
| 189 | $scope.getRevisions = function (credential) { |
||
| 190 | var _credential = angular.copy(credential); |
||
| 191 | $rootScope.$emit('app_menu', false); |
||
| 192 | SettingsService.setSetting('revision_credential', CredentialService.encryptCredential(_credential)); |
||
| 193 | $location.path('/vault/' + $scope.active_vault.guid + '/' + _credential.guid + '/revisions'); |
||
| 194 | }; |
||
| 195 | |||
| 196 | $scope.shareCredential = function (credential) { |
||
| 197 | var _credential = angular.copy(credential); |
||
| 198 | $rootScope.$emit('app_menu', false); |
||
| 199 | SettingsService.setSetting('share_credential', CredentialService.encryptCredential(_credential)); |
||
| 200 | $location.path('/vault/' + $scope.active_vault.guid + '/' + _credential.guid + '/share'); |
||
| 201 | }; |
||
| 202 | |||
| 203 | var notification; |
||
| 204 | $scope.deleteCredential = function (credential) { |
||
| 205 | var _credential = angular.copy(credential); |
||
| 206 | try { |
||
| 207 | _credential = CredentialService.decryptCredential(angular.copy(credential)); |
||
| 208 | } catch (e) { |
||
| 209 | |||
| 210 | } |
||
| 211 | _credential.delete_time = new Date().getTime() / 1000; |
||
| 212 | for (var i = 0; i < $scope.active_vault.credentials.length; i++) { |
||
| 213 | if ($scope.active_vault.credentials[i].credential_id === credential.credential_id) { |
||
| 214 | $scope.active_vault.credentials[i].delete_time = _credential.delete_time; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | $scope.closeSelected(); |
||
| 218 | if (notification) { |
||
| 219 | NotificationService.hideNotification(notification); |
||
| 220 | } |
||
| 221 | notification = NotificationService.showNotification('Credential deleted', 5000, |
||
| 222 | function () { |
||
| 223 | CredentialService.updateCredential(_credential).then(function (result) { |
||
| 224 | if (result.delete_time > 0) { |
||
| 225 | notification = false; |
||
| 226 | |||
| 227 | } |
||
| 228 | }); |
||
| 229 | }); |
||
| 230 | |||
| 231 | }; |
||
| 232 | |||
| 233 | $scope.recoverCredential = function (credential) { |
||
| 234 | var _credential = angular.copy(credential); |
||
| 235 | try { |
||
| 236 | _credential = CredentialService.decryptCredential(angular.copy(credential)); |
||
| 237 | } catch (e) { |
||
| 238 | |||
| 239 | } |
||
| 240 | for (var i = 0; i < $scope.active_vault.credentials.length; i++) { |
||
| 241 | if ($scope.active_vault.credentials[i].credential_id === credential.credential_id) { |
||
| 242 | $scope.active_vault.credentials[i].delete_time = 0; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | _credential.delete_time = 0; |
||
| 246 | $scope.closeSelected(); |
||
| 247 | if (notification) { |
||
| 248 | NotificationService.hideNotification(notification); |
||
| 249 | } |
||
| 250 | NotificationService.showNotification('Credential recovered', 5000, |
||
| 251 | function () { |
||
| 252 | CredentialService.updateCredential(_credential).then(function (result) { |
||
| 253 | notification = false; |
||
| 254 | |||
| 255 | }); |
||
| 256 | }); |
||
| 257 | |||
| 258 | }; |
||
| 259 | |||
| 260 | $scope.destroyCredential = function (credential) { |
||
| 261 | var _credential = angular.copy(credential); |
||
| 262 | CredentialService.destroyCredential(_credential.credential_id).then(function (result) { |
||
| 263 | for (var i = 0; i < $scope.active_vault.credentials.length; i++) { |
||
| 264 | if ($scope.active_vault.credentials[i].credential_id === credential.credential_id) { |
||
| 265 | $scope.active_vault.credentials.splice(i, 1); |
||
| 266 | NotificationService.showNotification('Credential destroyed', 5000); |
||
| 267 | break; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | }); |
||
| 271 | }; |
||
| 272 | |||
| 273 | $scope.view_mode = 'list'; //@TODO make this a setting |
||
| 274 | $scope.switchViewMode = function (viewMode) { |
||
| 275 | $scope.view_mode = viewMode; |
||
| 276 | }; |
||
| 277 | |||
| 278 | $scope.filterOptions = { |
||
| 279 | filterText: '', |
||
| 280 | fields: ['label', 'username', 'email', 'password', 'custom_fields'] |
||
| 281 | }; |
||
| 282 | |||
| 283 | |||
| 284 | $scope.filtered_credentials = []; |
||
| 285 | $scope.$watch('[selectedtags, filterOptions, delete_time, active_vault.credentials]', function () { |
||
| 286 | if (!$scope.active_vault) { |
||
| 287 | return; |
||
| 288 | } |
||
| 289 | if ($scope.active_vault.credentials) { |
||
| 290 | var credentials = angular.copy($scope.active_vault.credentials); |
||
| 291 | var filtered_credentials = $filter('credentialSearch')(credentials, $scope.filterOptions); |
||
| 292 | filtered_credentials = $filter('tagFilter')(filtered_credentials, $scope.selectedtags); |
||
| 293 | filtered_credentials = $filter('filter')(filtered_credentials, {hidden: 0}); |
||
| 294 | $scope.filtered_credentials = filtered_credentials; |
||
| 295 | } |
||
| 296 | }, true); |
||
| 297 | |||
| 298 | $scope.selectedtags = []; |
||
| 299 | var to; |
||
| 300 | $rootScope.$on('selected_tags_updated', function (evt, _sTags) { |
||
| 301 | var _selectedTags = []; |
||
| 302 | for (var x = 0; x < _sTags.length; x++) { |
||
| 303 | _selectedTags.push(_sTags[x].text); |
||
| 304 | } |
||
| 305 | $scope.selectedtags = _selectedTags; |
||
| 306 | $timeout.cancel(to); |
||
| 307 | if (_selectedTags.length > 0) { |
||
| 308 | to = $timeout(function () { |
||
| 309 | if ($scope.filtered_credentials) { |
||
| 310 | var _filtered_tags = []; |
||
| 311 | for (var i = 0; i < $scope.filtered_credentials.length; i++) { |
||
| 312 | var tags = $scope.filtered_credentials[i].tags_raw; |
||
| 313 | for (var x = 0; x < tags.length; x++) { |
||
| 314 | var tag = tags[x].text; |
||
| 315 | if (_filtered_tags.indexOf(tag) === -1) { |
||
| 316 | _filtered_tags.push(tag); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | $rootScope.$emit('limit_tags_in_list', _filtered_tags); |
||
| 322 | } |
||
| 323 | }, 50); |
||
| 324 | } |
||
| 325 | }); |
||
| 326 | |||
| 327 | $scope.delete_time = 0; |
||
| 328 | $scope.showCredentialRow = function (credential) { |
||
| 329 | if ($scope.delete_time === 0) { |
||
| 330 | return credential.delete_time === 0; |
||
| 331 | } else { |
||
| 332 | return credential.delete_time > $scope.delete_time; |
||
| 333 | } |
||
| 334 | |||
| 335 | }; |
||
| 336 | |||
| 337 | $rootScope.$on('set_delete_time', function (event, time) { |
||
| 338 | $scope.delete_time = time; |
||
| 339 | }); |
||
| 340 | |||
| 341 | $scope.setDeleteTime = function (delete_time) { |
||
| 342 | $scope.delete_time = delete_time; |
||
| 343 | }; |
||
| 344 | |||
| 345 | $scope.selectedCredential = false; |
||
| 346 | $scope.selectCredential = function (credential) { |
||
| 347 | $scope.selectedCredential = angular.copy(credential); |
||
| 348 | $rootScope.$emit('app_menu', true); |
||
| 349 | }; |
||
| 350 | |||
| 351 | $scope.closeSelected = function () { |
||
| 352 | $rootScope.$emit('app_menu', false); |
||
| 353 | $scope.selectedCredential = false; |
||
| 354 | }; |
||
| 355 | |||
| 356 | $rootScope.$on('logout', function () { |
||
| 357 | $scope.active_vault = null; |
||
| 358 | $scope.credentials = []; |
||
| 359 | // $scope.$parent.selectedVault = false; |
||
| 360 | |||
| 361 | }); |
||
| 362 | |||
| 363 | |||
| 364 | $scope.downloadFile = function (credential, file) { |
||
| 365 | var callback = function (result) { |
||
| 366 | var key = null; |
||
| 367 | if (!result.hasOwnProperty('file_data')) { |
||
| 368 | NotificationService.showNotification('Error downloading file, you probably don\'t have enough permissions', 5000); |
||
| 369 | return; |
||
| 370 | |||
| 371 | } |
||
| 372 | if (!credential.hasOwnProperty('acl') && credential.hasOwnProperty('shared_key')) { |
||
| 373 | if (credential.shared_key) { |
||
| 374 | key = EncryptService.decryptString(angular.copy(credential.shared_key)); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | if (credential.hasOwnProperty('acl')) { |
||
| 378 | key = EncryptService.decryptString(angular.copy(credential.acl.shared_key)); |
||
| 379 | } |
||
| 380 | |||
| 381 | var file_data = EncryptService.decryptString(result.file_data, key); |
||
| 382 | download(file_data, escapeHTML(file.filename), file.mimetype); |
||
| 383 | |||
| 384 | }; |
||
| 385 | |||
| 386 | if (!credential.hasOwnProperty('acl')) { |
||
| 387 | FileService.getFile(file).then(callback); |
||
| 388 | } else { |
||
| 389 | ShareService.downloadSharedFile(credential, file).then(callback); |
||
| 390 | } |
||
| 391 | |||
| 392 | }; |
||
| 393 | |||
| 394 | }]); |
||
| 395 | }()); |